package edu.bluebeanforum.controller;

import edu.bluebeanforum.common.AppResult;
import edu.bluebeanforum.common.ResultCode;
import edu.bluebeanforum.config.AppConfig;
import edu.bluebeanforum.model.Message;
import edu.bluebeanforum.model.User;
import edu.bluebeanforum.service.IMessageService;
import edu.bluebeanforum.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.actuate.trace.http.HttpTrace;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "站内信接口")
@RestController
@RequestMapping("/message")
public class MessageController {

    @Resource
    private IMessageService messageService;
    @Resource
    private IUserService userService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接受者id") @RequestParam("receiveUserId")  @NonNull Long receiveUserId,
                          @ApiParam("内容") @RequestParam("content")  @NonNull String content){
        //获取当前登录用户的信息  ——从session中取出
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        //校验当前登录用户的状态，如果是禁言就不能发表站内信
        if(user.getState()==1){
            return AppResult.fail(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发送站内信
        if (user.getId()==receiveUserId){
            return AppResult.fail("不可以给自己发送站内信");
        }
        //校验接受者是否存在
        User recevieUser=userService.selectById(receiveUserId);
        if(recevieUser==null||recevieUser.getDeleteState()==1){
            return AppResult.fail("接受者状态异常");
        }
        //封装对象
        Message message=new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        messageService.create(message);

        return AppResult.success("发送成功");
    }


    @ApiOperation("获取未读数")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        Integer count = messageService.selectUnreadCount(user.getId());//当前登录用户的id就是接受者
        return AppResult.success(count);
    }

    @ApiOperation("查询用户的所有站内信")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());

        return AppResult.success(messages);
    }
    @ApiOperation("更新为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @ApiParam("站内信id")  @RequestParam("id") @NonNull Long id){
        //1、根据id查询站内信
        Message message = messageService.selectById(id);
        //2、站内信是否存在
        if(message==null||message.getDeleteState()==1){
            return AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //3、站内信是不是自己的
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getId()!=message.getReceiveUserId()){
            return AppResult.fail(ResultCode.FAILED_FORBIDDEN);
        }
        messageService.updateStateById(id, (byte) 1);
        return AppResult.success();
    }

    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("要回复的站内信id") @RequestParam("repliedId") @NonNull Long repliedId,
                           @ApiParam("站内信的内容") @RequestParam("content") @NonNull String content){
        //校验当前登录用户的状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1){
            return AppResult.fail(ResultCode.FAILED_USER_NOT_EXISTS);
        }
        //校验要回复的站内信状态
        Message existsMessage=messageService.selectById(repliedId);
        if(existsMessage==null||existsMessage.getDeleteState()==1){
            return AppResult.fail(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能给自己回复
        if(user.getId()==existsMessage.getPostUserId()){
            return AppResult.fail("不可以给自己的站内信回复");
        }
        //构造对象
        Message message=new Message();
        message.setPostUserId(user.getId());//发送者
        message.setReceiveUserId(existsMessage.getPostUserId());//接受者
        message.setContent(content);
        messageService.reply(repliedId,message);
        return AppResult.success();
    }

}

